package cn.jiedh.app.meiweidk.loan;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RatingBar;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import cn.jiedh.app.meiweidk.R;
import cn.jiedh.app.meiweidk.Utils.tool.UrlUtils;
import cn.jiedh.app.meiweidk.base.BaseFragment;
import cn.jiedh.app.meiweidk.baseadapter.BaseAdapter;
import cn.jiedh.app.meiweidk.baseadapter.BaseViewHolder;
import cn.jiedh.app.meiweidk.entity.LoanDetailCommentBean;
import cn.jiedh.app.meiweidk.network.ResponseSuccess;
import cn.jiedh.app.meiweidk.response.BaseResponse;

/**
 * @name：产品详情评论tab页
 * @author：司马林
 * @phone：18423134135
 * @createTime：2017/10/23
 */

public class LoanDetailCommentFragment extends BaseFragment {
    @BindView(R.id.loan_detail_comment_rv)
    RecyclerView rv;
    @BindView(R.id.smartrefreshLayout)
    SmartRefreshLayout smartrefreshLayout;
    View comprehensiveGroup;

    private int productId;
    private String iconUrl;
    private String loanTitle;
    private String loanAdvert;
    private int pageSize = 10;
    private int mPageNo = 1;

    private BaseAdapter<LoanDetailCommentBean.DataList> adapter;
    private List<LoanDetailCommentBean.DataList> commentList = new ArrayList<>();

    private TextView comprehensiveScoreTv;
    private RatingBar ratingBar;
    private TextView interestRateTv;
    private TextView quotaTv;
    private TextView speedTv;

    private double comprehensiveScore;
    private double intersetRate;
    private double quota;
    private double speed;

    View headerView;

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_loan_detail_comment;
    }

    @Override
    protected void initView(View view) {
        if (getArguments() != null) {
            productId = getArguments().getInt("productId");
            iconUrl = getArguments().getString("iconUrl");
            loanTitle = getArguments().getString("loanTitle");
            loanAdvert = getArguments().getString("loanAdvert");
        }
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        smartrefreshLayout.setEnableLoadmoreWhenContentNotFull(true);
        smartrefreshLayout.setOnLoadmoreListener(refreshLayout -> {
            refreshLayout.getLayout().postDelayed(() -> {
                mPageNo++;
                getData(mPageNo);
                refreshLayout.finishLoadmore();
            }, 2000);
        });
        adapter = new BaseAdapter<LoanDetailCommentBean.DataList>(getActivity(), R.layout.loan_detail_comment_item, commentList) {
            @Override
            public void convert(BaseViewHolder holder, LoanDetailCommentBean.DataList bean, int position) {
                String userPhone = bean.getUserPhone();
                if (!TextUtils.isEmpty(userPhone)) {
                    holder.setText(R.id.comment_user, "用户" + userPhone);
                } else {
                    holder.setText(R.id.comment_user, "");
                }
                holder.setText(R.id.comment_time, bean.getEvaluateTime());
                holder.setText(R.id.comment_content, bean.getContent());
                RatingBar ratingBar = holder.getView(R.id.comment_rating_bar);

                int comprehensiveSore = (int) bean.getComprehensiveScore();
                ratingBar.setRating(comprehensiveSore);
                TextView content = holder.getView(R.id.comment_content);
                content.setOnClickListener(v -> {
                    if (content.getEllipsize() == TextUtils.TruncateAt.END) {
                        content.setEllipsize(null);
                        content.setMaxLines(Integer.MAX_VALUE);
                    }
                });
            }
        };
        headerView = LayoutInflater.from(getActivity()).inflate(R.layout.loan_detail_comment_header, rv, false);
        adapter.setHeaderView(headerView);
        initHeader(headerView);
        rv.setAdapter(adapter);
        comprehensiveGroup=headerView.findViewById(R.id.comprehensive_group);
    }

    private void initHeader(View view) {
        comprehensiveScoreTv = view.findViewById(R.id.comprehensive_score_tv);
        interestRateTv = view.findViewById(R.id.interest_rate);
        quotaTv = view.findViewById(R.id.quota);
        speedTv = view.findViewById(R.id.speed);
        ratingBar = view.findViewById(R.id.comprehensive_score_bar);

    }

    @Override
    protected void initData() {
        getData(1);
    }

    /**
     * @param pageNo 第几页
     */
    private void getData(int pageNo) {
        showLoadingDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("productId", productId);
        map.put("pageSize", pageSize);
        map.put("pageNo", pageNo);
        request(UrlUtils.COMMENTLIST, map, false, new ResponseSuccess<LoanDetailCommentBean>() {
            @Override
            public void onRequestSucceed(BaseResponse<LoanDetailCommentBean> response) {
                dismissLoadingDialog();
                if (0 == response.getCode()) {
                    if (response.getData().getDataList().size() <= 0) {
                        mPageNo = pageNo - 1;
                    }
                    comprehensiveGroup.setVisibility(View.VISIBLE);
                    comprehensiveScore = response.getData().getComprehensiveScore();
                    intersetRate = response.getData().getRateScore();
                    quota = response.getData().getQuotaScore();
                    speed = response.getData().getSpeedScore();
                    commentList.addAll(response.getData().getDataList());
                    comprehensiveScoreTv.setText(comprehensiveScore + "");
                    interestRateTv.setText("产品利率：" + intersetRate);
                    quotaTv.setText("贷款额度：" + quota);
                    speedTv.setText("放款速度" + speed);
                    ratingBar.setRating((int) comprehensiveScore);
                    if (commentList.size() == 0) {
                        headerView.setVisibility(View.GONE);
                    }
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

}
